package com.ysy.logistics.controller.stock;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.stock.StoreHouse;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.stock.StoreHouseService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/31 14:46
 **/
@Slf4j
@RestController
@RequestMapping(value = "/storeHouse",name = "仓库")
@Tag(name = "仓库管理",description = "storeHouse表的CRUD")
@CrossOrigin
public class StoreHouseController {

    @Resource
    private StoreHouseService storeHouseService;

    @PostMapping(value = "/listByPage")
    @Operation(summary = "分页查询仓库",description = "分页查询仓库")
    @OperateLog(value = "查询仓库")
    public ResultData<Page<StoreHouse>> listByPage(@RequestBody PageInfo<StoreHouse> pageInfo){
        return ResultData.success(storeHouseService.listByPage(pageInfo));
    }

    @PostMapping(value = "/save")
    @Operation(summary = "保存/更新仓库",description = "保存/更新仓库")
    @OperateLog(value = "保存/更新仓库")
    public ResultData<StoreHouse> save(@Validated @RequestBody StoreHouse storeHouse){

        if (StringUtils.isBlank(storeHouse.getId())){
            storeHouse.setCreateTime(new Date());
            storeHouse.setStatus("1");
            storeHouseService.save(storeHouse);
        }else {
            //已使用容量避免更改
            storeHouse.setUsedCapacity(null);
            storeHouse.setCreateTime(null);
            //修改后的容量不能小于已使用容量
            boolean capacityFlag = Objects.nonNull(storeHouse.getCapacity());
            boolean usedCapacityFlag = Objects.nonNull(storeHouse.getUsedCapacity());
            if (usedCapacityFlag && capacityFlag && storeHouse.getCapacity() < storeHouse.getUsedCapacity()){
                return ResultData.fail("容量不能小于已使用容量");
            }
            storeHouseService.updateById(storeHouse);
        }
        return ResultData.success(storeHouse);
    }

    @GetMapping(value = "/changeStatus/{id}")
    @Operation(summary = "修改仓库状态",description = "修改仓库状态")
    @OperateLog(value = "修改仓库状态")
    public ResultData<StoreHouse> changeStatus(@PathVariable("id") String id){
        StoreHouse storeHouse = storeHouseService.getById(id);
        if (Objects.isNull(storeHouse)){
            return ResultData.fail("仓库不存在");
        }

        StoreHouse update = new StoreHouse();
        update.setId(id);
        if (storeHouse.getStatus().equals("0")){
            //有容量不能停用
            if (storeHouse.getUsedCapacity() > 0){
                return ResultData.fail("该仓库使用中不能停用");
            }
            update.setStatus("1");
        }else {
            update.setStatus("0");
        }
        storeHouseService.updateById(update);
        return ResultData.success();
    }

    @GetMapping(value = "/delete/{id}")
    @Operation(summary = "删除仓库",description = "删除仓库")
    @OperateLog(value = "删除仓库")
    public ResultData<StoreHouse> delete(@PathVariable("id") String id){
        StoreHouse storeHouse = storeHouseService.getById(id);
        if (Objects.isNull(storeHouse)){
            return ResultData.fail("仓库不存在");
        }
        //有容量不能删除
        if (storeHouse.getUsedCapacity() > 0){
            return ResultData.fail("该仓库使用中不能删除");
        }
        //启用中不能删除
        if (storeHouse.getStatus().equals("0")){
            return ResultData.fail("仓库状态为启用，不能删除");
        }
        storeHouseService.removeById(id);
        return ResultData.success();
    }

    @GetMapping(value = "/list/{needCapacity}")
    @Operation(summary = "根据容量查询可用仓库",description = "根据容量查询可用仓库")
    public ResultData< List<StoreHouse> > list(@PathVariable("needCapacity") Integer needCapacity){
        List<StoreHouse> canUsedList = storeHouseService.getCanUsedList(needCapacity);
        if (Objects.nonNull(canUsedList)){
            return ResultData.success(canUsedList);
        }
        return ResultData.fail("没有可用仓库，请添加仓库");
    }

    @GetMapping(value = "/getHouseList")
    @Operation(summary = "获取仓库列表",description = "获取仓库列表")
    public ResultData<List<StoreHouse>> getHouseList(){
        LambdaQueryWrapper<StoreHouse> lqw = new LambdaQueryWrapper<>();
        lqw.select(StoreHouse::getId,StoreHouse::getName)
        .eq(StoreHouse::getStatus, StatusEnum.ENABLE.getCode());
        List<StoreHouse> list = storeHouseService.list(lqw);
        return ResultData.success(list);
    }
}
